import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User } from './entities/user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private userRepository: Repository<User>
  ) {
  }

  async create(createUserDto: CreateUserDto) {
    const user = this.userRepository.create(createUserDto);

    const exitUser = await this.userRepository.findOne({
        where: {
          userName: user.userName,
        },
      });
  
      if(exitUser) {
          return null
      }

    const saveUser = await this.userRepository.save(user);

    return saveUser
  }

  async findAll() {
    const users = await this.userRepository.find();

    return users
  }

  async findOne(userName: string) {
    const user = await this.userRepository.findOne({
        where: {
          userName,
        },
      });

      if(!user) {
          return null
      }

      return user
  }

  update(id: number, updateUserDto: UpdateUserDto) {
    return `This action updates a #${id} user`;
  }

  remove(id: number) {
    return `This action removes a #${id} user`;
  }
}
